<?php
function friendlyDate($sTime,$type = 'normal',$alt = 'false') {
    if(!$sTime) {
        return '';
    }
    //sTime=源时间，cTime=当前时间，dTime=时间差
    $cTime		=	time();
    $dTime		=	$cTime - $sTime;
    $dDay		=	intval(date("Ymd",$cTime)) - intval(date("Ymd",$sTime));
    $dYear		=	intval(date("Y",$cTime)) - intval(date("Y",$sTime));
    //normal：n秒前，n分钟前，n小时前，日期
    if($type=='normal') {
        if( $dTime < 60 ) {
            return $dTime."秒前";
        }elseif( $dTime < 3600 ) {
            return intval($dTime/60)."分钟前";
        }elseif( $dTime >= 3600 && $dDay == 0  ) {
            return intval($dTime/3600)."小时前";
        }elseif($dYear==0) {
            return date("m-d H:i:s",$sTime);
        }else {
            return date("y-m-d H:i:s",$sTime);
        }
    //full: Y-m-d , H:i:s
    }elseif($type=='full') {
        return date("y-m-d H:i",$sTime);
    }elseif($type=='month') {
        return date("m-d H:i",$sTime);
    }else {
        if( $dTime < 60 ) {
            return $dTime."秒前";
        }elseif( $dTime < 3600 ) {
            return intval($dTime/60)."分钟前";
        }elseif( $dTime >= 3600 && $dDay == 0  ) {
            return intval($dTime/3600)."小时前";
        }elseif($dYear==0) {
            return date("y-m-d H:i",$sTime);
        }else {
            return date("y-m-d H:i",$sTime);
        }
    }
}
//入库前的过滤
function  new_addslashes($string) {
	if(!get_magic_quotes_gpc()){
		    if(!is_array($string)) return addslashes(trim($string));
	    foreach($string as $key => $val) $string[$key] = new_addslashes($val);
	    return $string;
	}else{
		return $string;
	}
}

function strlen_utf8($str) {   
 $i = 0;   
 $count = 0;   
 $len = strlen ($str);   
 while ($i < $len) {   
	 $chr = ord ($str[$i]);   
	 $count++;   
	 $i++;   
	 if($i >= $len) break;   
	 if($chr & 0x80) {   
		 $chr <<= 1;   
		 while ($chr & 0x80) {   
		 $i++;   
		 $chr <<= 1;   
		 }   
	 }   
 }   
 return $count;   
}
function titleCut($num,$tie){
	  $str=$tie;
	  $start=0;
	  $length=$num;
	  $charset="utf-8";
	  if(strlen_utf8($str)<=$length){
	  	return $str;
	  }else{
	  	$length = $num-3;
	  }
	   if(function_exists("mb_substr"))
        return mb_substr($str, $start, $length, $charset).'...';
    elseif(function_exists('iconv_substr')) {
        return iconv_substr($str,$start,$length,$charset).'...';
    }
    $re['utf-8']   = "/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xff][\x80-\xbf]{3}/";
    $re['gb2312'] = "/[\x01-\x7f]|[\xb0-\xf7][\xa0-\xfe]/";
    $re['gbk']	  = "/[\x01-\x7f]|[\x81-\xfe][\x40-\xfe]/";
    $re['big5']	  = "/[\x01-\x7f]|[\x81-\xfe]([\x40-\x7e]|\xa1-\xfe])/";
    preg_match_all($re[$charset], $str, $match);
    $slice = join("",array_slice($match[0], $start, $length));
    $slice= $slice.'...';
    return $slice;
       
}

function cleanJs($text) {
    $text	=	trim($text);
    //$text	=	mysql_escape_string($text);
    $text	=	stripslashes($text);
    //完全过滤注释
    $text	=	preg_replace('/<!--?.*-->/','',$text);
    //完全过滤动态代码
    $text	=	preg_replace('/<\?|\?>/','',$text);
    //完全过滤js
    $text	=	preg_replace('/<script?.*\/script>/','',$text);
    //过滤多余html
    $text	=	preg_replace('/<\/?(html|head|meta|link|base|body|title|style|script|form|iframe|frame|frameset)[^><]*>/i','',$text);
    //过滤on事件lang js
    while(preg_match('/(<[^><]+)(lang|onfinish|onmouse|onexit|onerror|onkey|onload|onchange|onfocus|onblur|style)[^><]+/i',$text,$mat)) {
        $text=str_replace($mat[0],$mat[1],$text);
    }
    while(preg_match('/(<[^><]+)(window\.|js:|javascript:|about:|file:|document\.|vbs:|vbscript:|cookie)([^><]*)/i',$text,$mat)) {
        $text=str_replace($mat[0],$mat[1].$mat[3],$text);
    }
    //过滤多余空格
    $text	=	str_replace('  ',' ',$text);
    return $text;
}
//纯文本输出
function t($text) {
    $text	=	cleanJs($text);
    $text	=	strip_tags($text);
    $text	=	htmlspecialchars($text,ENT_NOQUOTES);
    return $text;
}
//输出安全的html
function showHtml($text) {
    $text	=	cleanJs($text);
    return $text;
}
function upload($filename,$saveName=uniqid){
	
	    import("ORG.Net.UploadFile");   
		  $upload = new UploadFile(); // 实例化上传类   
		  $upload->maxSize  = 3145728 ; // 设置附件上传大小   
		  $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg'); // 设置附件上传类型
		  $upload->saveRule =  $saveName;
		  $upload->savePath =  UPLOAD_PATH; // 设置附件上传目录   
		  if($filename){
		  	$upload->savePath = UPLOAD_PATH.$filename.'/';
		  }
		  $info = null;
		  if(!$upload->upload()) { // 上传错误 提示错误信息   
		    $this->error($upload->getErrorMsg());   
		  }else{ // 上传成功 获取上传文件信息   
		    $info =  $upload->getUploadFileInfo();
		  }
		  
		  return $info;
	
}
//获得表情数组
function getBiaoQing() { 
    $xml = simplexml_load_file(SITE_PATH."Public/images/biaoqing/mini.xml");
    $xml_array = array();
		foreach($xml->children() as $child)
		{
		 $temp['title']=$child->title;
		 $temp['emotion']=$child->emotion;
		 $temp['filename']=$child->filename;
		 array_push($xml_array,$temp);
		}
      
    return($xml_array); 
}

function pageContent($content){
	
	return str_replace('<p><img src="http://localhost/yxxh/Public/plug/editor/skins/default/separate.gif" /></p>','</div><div class="result">',$content);

}
//处理编码字符
function CkPostStrChar($val){
	if(substr($val,-1)=="\\")
	{
		exit();
	}
}
//处理提交字符
function RepPostStr($val,$ecms=0){
	$val=htmlspecialchars($val,ENT_QUOTES);
	if($ecms==0)
	{
		CkPostStrChar($val);
	}
	return $val;
}
//参数处理函数
function RepPostVar($val){
	if($val!=addslashes($val))
	{
		exit();
	}
	CkPostStrChar($val);
	$val=str_replace(" ","",$val);
	$val=str_replace("%20","",$val);
	$val=str_replace("%27","",$val);
	$val=str_replace("*","",$val);
	$val=str_replace("'","",$val);
	$val=str_replace("\"","",$val);
	$val=str_replace("/","",$val);
	$val=str_replace(";","",$val);
	$val=str_replace("#","",$val);
	$val=str_replace("--","",$val);
	$val=RepPostStr($val,1);
	$val=addslashes($val);
	return $val;
} 


?>